package org.jiucheng.magpiebridge.server.aio.proxy;

import java.nio.channels.CompletionHandler;

public class ProxyClientWriteCompletionHandler implements CompletionHandler<Integer, ProxyClientWriteAttachment> {
    
    public void completed(Integer result, ProxyClientWriteAttachment attachment) {
        if (attachment.getWriteByteBuffer().hasRemaining()) {
            attachment.getAsynchronousSocketChannel().write(attachment.getWriteByteBuffer(), attachment, this);
            return;
        }
        attachment.writed.compareAndSet(true, false);
    }

    public void failed(Throwable exc, ProxyClientWriteAttachment attachment) {
        attachment.writed.compareAndSet(true, false);
        exc.printStackTrace();
    }
}
